package com.njts.programming;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/**
 * @author David GE
 * Date  2025/11/17
 * 开发一个程序，可以生成指定位数的验证码，每位可以是数字、大小写字母
 */
public class VerifyCode {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入验证码的位数");
        int digit = scanner.nextInt();
        // 一般来说,验证码4位或6位
        char[] code = VerifyCode.getVerifyCode(digit);
        System.out.println("code:"+ Arrays.toString(code));
    }
    public static char[] getVerifyCode(int digit){
        char[] code = new char[digit];
        Random random = new Random();

        for (int i = 0; i < code.length; i++) {
            int type = random.nextInt(3);
            // 随机生成的字符:Random
            switch (type){
                // 数字
                case 0:
                    code[i]= (char) ('0'+random.nextInt(10));
                    break;
                case 1:
                    //大写字母
                    code[i]= (char) ('A'+random.nextInt(26));
                    break;
                default:
                    //小写字母
                    code[i]= (char) ('a'+random.nextInt(26));
                    break;
            }

        }
        return code;
    }
}
